﻿using KTD.Web.Models;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XXS.Common.BaseDataAccessLayer;
using XXS.Common.Paginate;

namespace KTD.Web.BLL
{
    public class SalerManager
    {

        private readonly IBaseDAL<Saler> _SalerDal;
        private readonly IBaseDAL<Member> _MemberDal;
        private readonly IBaseDAL<House> _HouseDal;
        public SalerManager()
        {
            _SalerDal = new BaseDAL<Saler>(new DB());
            _MemberDal = new BaseDAL<Member>(new DB());
            _HouseDal = new BaseDAL<House>(new DB());
        }

        /// <summary>
        /// 获取销售人员列表
        /// </summary>
        /// <param name="im"></param>
        /// <returns></returns>
        public virtual IPaginate<SalerVD> GetListBy(SalerQD im)
        {
            var res = _SalerDal.FindList();
            if (im.MemberId != null&&im.MemberId != 0)
            {
                res = res.Where(n => n.memberId == im.MemberId);
            }
            if (im.Status != null)
            {
                res = res.Where(n => n.status == im.Status);
            }
            if (!string.IsNullOrEmpty(im.Type))
            {
                res = res.Where(n => n.type==im.Type);
            }
            if (im.HouseId != null)
            {
                res = res.Where(n => n.houseid == im.HouseId);
            }
            if (!string.IsNullOrEmpty(im.Query))
            {
                res = res.Where(n => n.name.Contains(im.Query) || n.housename.Contains(im.Query));
            }
            if (im.cityId != null)
            {
                res = res.Where(n =>n.cityId==im.cityId);
            }
            var list = res.OrderByDescending(n=>n.hot).ThenByDescending(n=>n.click)
                .ToPaginate(im, n => new SalerVD().MapFrom(n));
            return list;
        }

        /// <summary>
        /// 新建销售人员
        /// </summary>
        /// <param name="im"></param>
        public void Create(SalerCD im)
        {
            var old = _SalerDal.Find(im.Id);
            if (old != null)
            {
                return;
            }
            Saler model = new Saler();
            im.MapTo(model);
            //var member = _MemberDal.Find(n => n.Id == im.memberId);
            // model.Creator = string.IsNullOrEmpty(member.name) ? member.wechaname : member.name;
            var house = _HouseDal.FindList(n => n.housename == im.housename).FirstOrDefault();
            model.houseid = house.Id;
            if (string.IsNullOrEmpty(model.avatar))
            {
                model.avatar = "/Attachment/head.png";
            }
            model.cityId = house.cityid;
            _SalerDal.Create(model);
        }

        /// <summary>
        /// 更新销售人员（审核）
        /// </summary>
        /// <param name="im"></param>
        public void Update(SalerCD im)
        {
            Saler model = _SalerDal.Find(im.Id);
            var avatar = model.avatar;
            var code = model.code;
            var gp = model.gp;
            //model.status = im.status;
            //model.hot = im.hot;

            im.MapTo(model);
            if (string.IsNullOrEmpty(im.avatar))
            {
                model.avatar = avatar;
            }
            if (string.IsNullOrEmpty(im.code))
            {
                model.code = code;
            }
            if (string.IsNullOrEmpty(im.gp))
            {
                model.gp = gp;
            }
            var house = _HouseDal.FindList(n => n.housename == im.housename).FirstOrDefault();
            model.houseid = house.Id;
            model.cityId = house.cityid;
            _SalerDal.Update(model);
        }



        /// <summary>
        /// 更新销售人员（审核）
        /// </summary>
        /// <param name="im"></param>
        public void Like(SalerLikeD im)
        {
            Saler model = _SalerDal.Find(im.Id);
            if (im.isLike)
            {
                model.zan = model.zan + 1;
            }
            else
            {
                if(model.zan!=0)
                {
                    model.zan = model.zan - 1;
                }
            }
            _SalerDal.Update(model);
        }
        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public SalerVD Find(int id)
        {
            Saler model = _SalerDal.Find(id);
         
            model.click++;
            _SalerDal.Update(model);
            
         
            return new SalerVD().MapFrom(model);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            Saler model = _SalerDal.Find(id);
            if (model == null)
            {
                throw new Exception("错误的ID");
            }
            _SalerDal.Delete(model);
        }
    }
}